# To change this template, choose Tools | Templates
# and open the template in the editor.





import wx
import os

import queVars


class SettingsWin(wx.Frame):

	cookiePlugins = []




	def __init__(self, *args, **kwds):

		self.lparent = args[-1]				#Add pointer to main window to self for easy access
		args = args[:-1]				#And remove main window pointer from args
		self.lparent.Disable()

		# begin wxGlade: MainW.__init__
		kwds["style"] = wx.DEFAULT_FRAME_STYLE|wx.STAY_ON_TOP|wx.TAB_TRAVERSAL
		wx.Frame.__init__(self, *args, **kwds)


		self.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
		self.SetBackgroundColour(wx.Colour(240, 240, 240))
		self.SetForegroundColour(wx.Colour(0, 0, 0))

		self.__set_properties()


		settingsItems = [
			self.__downloadDirSettingPicker,
			self.__getHorizontalRule,
			self.__settingsExitButtonSizer
			]

		bodySizer = wx.BoxSizer(wx.VERTICAL)
		self.settingsHeadLabel = wx.StaticText(self, -1, "Settings")
		self.settingsHeadLabel.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))

		bodySizer.Add(self.settingsHeadLabel, 0, wx.ALL, 5)

		for item in settingsItems:
			bodySizer.Add(item(), 0, wx.EXPAND, 5)


		self.SetSizer(bodySizer)

		self.Bind(wx.EVT_CLOSE, self.closeWindow)

		self.Fit()
		self.Layout()
		self.downloadPathChanged(None)
		
	def __settingsExitButtonSizer(self):


		tempControlSizer = wx.BoxSizer(wx.HORIZONTAL)

		tempControlSizer.Add((10,10), 1, wx.EXPAND, 5)

		self.exitSettingsWindowButton = wx.Button(self, -1, "Ok")
		tempControlSizer.Add(self.exitSettingsWindowButton, 0, wx.ALL, 5)


		self.Bind(wx.EVT_BUTTON, self.closeWindow, self.exitSettingsWindowButton)

		return tempControlSizer
		
	def __getHorizontalRule(self):
		return  wx.StaticLine(self, -1)
	
	def __set_properties(self):
		# begin wxGlade: MainW.__set_properties
		self.SetTitle("xADownloader Settings")
		self.SetSize((800, 800))

		# end wxGlade

	def __downloadDirSettingPicker(self):


		tempControlSizer = wx.BoxSizer(wx.HORIZONTAL)

		tempControlSizer.Add((10,10))

		self.downloadDirLabel = wx.StaticText(self, -1, "Download files to:")
		self.downloadDirLabel.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))
		tempControlSizer.Add(self.downloadDirLabel, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

		self.downloadDirField = wx.TextCtrl(self, -1, queVars.configuration.downloadDir)
		self.downloadDirField.SetMinSize((250, 20))
		tempControlSizer.Add(self.downloadDirField, 1, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)


		self.downloadDirDirPickerButton = wx.Button(self, -1, "...")
		self.downloadDirDirPickerButton.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "MS Shell Dlg 2"))
		tempControlSizer.Add(self.downloadDirDirPickerButton, 0, wx.ALL, 5)




		self.Bind(wx.EVT_BUTTON, self.selectDownloadDirEvt, self.downloadDirDirPickerButton)
		self.Bind(wx.EVT_TEXT, self.downloadPathChanged, self.downloadDirField)


		return tempControlSizer


	def selectDownloadDirEvt(self, event): # wxGlade: MainFrame.<event_handler>

		self.selectDownloadDir = wx.DirDialog (self, message = "Select Folder")
		self.selectDownloadDir.SetPath(queVars.configuration.downloadDir)
		if self.selectDownloadDir.ShowModal() == wx.ID_OK:
			newPath = self.selectDownloadDir.GetPath()
			self.downloadDirField.SetValue(newPath)

		self.downloadPathChanged(None)
		event.Skip()

	def downloadPathChanged(self, event):
		newPath = self.downloadDirField.Value
		if os.access(newPath, os.W_OK):
			self.downloadDirField.SetBackgroundColour(wx.Colour(220,255,220))
		else:
			self.downloadDirField.SetBackgroundColour(wx.Colour(255,220,220))

		self.Refresh()

	def closeWindow(self, event): # wxGlade: MainW.<event_handler>
		#print "Exiting!"
		newPath = self.downloadDirField.Value
		if os.access(newPath, os.W_OK):
			queVars.configuration.downloadDir = newPath

			self.Destroy()
			self.lparent.Enable()
			#print "Parent Enabled"

			print "Closing Settings Manager"
		else:
			errMessage = "Folder Path:\n'%s'\nis invalid. Please choose a valid folder path." % newPath
			self.invalidPathDialog = wx.MessageDialog (self, message = errMessage, style = wx.OK|wx.ICON_ERROR)
			self.invalidPathDialog.ShowModal()
		

# end of class MainW






